1. /* slflpow.cpp by K.Tsuru */
  2. // function ID 2002 DRADIX, BRADIX
  3. /**************************************************************
  4. SLong and SInteger classes
  5. It provides to the n-th powewr of x(x^n) Knuth's binary method.
  6. ***************************************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. #include <iostream>
  11. SLong Lpow(const SLong& x, ulong n){
  12. if(!x.Sign(2002)) return x; // x = 0
  13. int sgn = 1;
  14. uint h = x.Head();
  15. if(x.Sign() < 0) sgn = (n & 1) ? -1 : 1;
  16. if(!n) return SLong(x.Type(), minArraySize, 1L); // return 1;
  17. if(x.IsOne()) return SLong(x.Type(), minArraySize, sgn); // |x| = 1;
  18. fType rdx = x.Radix();
  19. //It estimates the number of figures which needs to store the result.
  20. //x has an order of x[h]*(R^h).
  21. ulong fig;
  22. fig = ulong( (double)n*(log10((double)x[h])/log10((double)rdx) + (double)h) )+1u;
  23. //figures in radix
  24. if(fig > x.MaxSize()) {
  25. cerr << "fig = " << fig << " n= " << n << endl;
  26. x.SetError(x.OVERFLOW_ERR,"Lpow", 2002);
  27. }
  28. /*
  29. The necessary figues is known, then it allocates its memory.
  30. If the out of memory error occures the program terminates here.
  31. */
  32. SLong y( x.Type(), (uint)fig), z( x.Type(), (uint)fig); // cutDown = DISABLE
  33. y = 1; z = x; //To make the size same as that of y it does not use z(x).
  34. if(x.Sign() < 0) z.ChangeSign(); // z > 0
  35. while(1){
  36. if( n & 1 ) y *= z;
  37. n /= 2;
  38. if( !n ) break;
  39. z *= z;
  40. }
  41. if(sgn < 0) y.ChangeSign();
  42. return y;
  43. }

slflpow.cpp : last modifiled at 2017/03/13 14:32:00(1,488 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).